<?php

namespace app\queue\redis\shushu;

use app\extensions\lib\push\BasePush;
use app\extensions\lib\push\Push;
use app\extensions\lib\ta_shushu\Shushu;
use app\model\Package;
use app\model\TokenInfo;
use app\queue\redis\in_page\InPush;
use support\Db;
use support\Log;
use support\Redis;
use Webman\RedisQueue\Consumer;

class RequestEvent implements Consumer
{
    // 要消费的队列名
    public $queue = 'queue_shushu_request_event_log';

    public $connection = 'default';



    // 消费
    public function consume($data)
    {
        $is_add = $data['is_add'] ?? true;
        $request_id = $data['request_id'] ?? '';
        $cate_id = $data['cate_id'] ?? '';
        $properties = $data['properties'] ?? [];

        $Shushu = new Shushu($cate_id);
        if($is_add){
            $params = $properties['params'] ?? [];unset($properties['params']);
            $properties['cate_id'] = $params['cate_id'] ?? '';
            $properties['game_id'] = $params['game_id'] ?? '';
            $properties['package_id'] = $params['package_id'] ?? '';
            if(empty($request_id) || empty($cate_id)){
                Log::channel('shushu')->info("RequestEvent:no request_id or cate_id".json_encode($data));
            }

            //计算请求需要的信息
            $uid_url = $params['udid_list_url'] ?? '';
            $udids = $params['udid'] ?? '';
            $budid_url = $params['budid_list_url'] ?? '';
            $budids = $params['budid'] ?? '';
            $sum = 0;
            $TokenInfo = new TokenInfo();
            //白名单数量
            if($uid_url){
                $sum = InPush::getUrlList($uid_url,true);
            }
            //指定udid
            if($udids){
                !is_array($udids) && $udids = explode(',',$udids);
                $sum += $TokenInfo->wherein('udid',$udids)->count();
            }

            //查询总数
            if(!$uid_url && !$udids){
                $sum = $TokenInfo->getUserList($params,1,0,[],'',true);
            }

            //黑名单名单数量
            if($budid_url){
                $cudids = Redis::lrange(Push::getBlackList($request_id),0,-1) ?:[];
                $sum -= count($cudids);
            }
            //指定budid
            if($budids){
                $sum -= $TokenInfo->wherein('udid',$budids)->count();
            }

            $properties['plat_count'] = $sum > 0 ? $sum : 0;//计划用户数
        }else{

            if($request_id){
                $start_date = date('Y-m-d', strtotime('-1 day'));
                $end_date = date('Y-m-d',strtotime($start_date)+86400*2);
                $event_name = BasePush::EVENT_SKD_REQUEST;
                $condition = "request_id = '$request_id' and \"#event_name\" = '$event_name'";

                $request_data = $Shushu->getEventId($start_date,$end_date,$condition);
                if(empty($request_data)){
                    if(isset($data['retry_count'])){
                        $data['retry_count']++;
                    }else{
                        $data['retry_count'] = 1;
                    }
                    if($data['retry_count']<100){
                        \Webman\RedisQueue\Redis::send($this->queue,$data,30);
                        return;
                    }else{
                        Log::channel('shushu')->warning("DeviceEvent_retry_error".json_encode($data));
                    }
                }
            }

        }




        $status = $Shushu->pushLog('track_update',$request_id, '', $properties,BasePush::EVENT_SKD_REQUEST,$request_id,$is_add);
        Log::channel('shushu')->info("RequestEvent-is_add:$is_add:status:$status".json_encode($data));


    }
}
